#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <vector>
#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>

using namespace std;

int RobotV_ = 0;
float RobotYawRate_ = 0;
/*-------------------------------------可改变量--------------------------------------------------*/

uint8_t enable_request_buffer[11]={0xfa, 0x00, 0x01,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xf5};//使能请求
uint8_t enable_buffer[11]= {0xf5, 0x00, 0x01,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xfa};//使能命令
uint8_t reset_buffer[11] = {0xf5, 0x01, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xfa};//回0位，速度0命令
uint8_t move_buffer[11]  = {0xf5, 0x01, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xfa};//运动命令（参数随话题改变）
uint8_t stop_buffer[11]  = {0xf5, 0x02, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xfa};//停止命令
int Header_len = 1;     //帧头长度
int Frame_len = 11;      //帧长度

/*--------------------------------------无须改动变量---------------------------------------------*/
std::vector<uint8_t> new_buffer(11, 0x00);//新读取的帧
std::vector<uint8_t> last_remaining_buffer(11, 0x00);//上一帧保留数据
std::vector<uint8_t> cat_buffer(22, 0x00); //cat_buffer是空的，拼接后的帧
uint8_t received_buffer[11]; //真正接收到的处理好的帧

////int data_len = cat_buffer.size();//拼接后的数据长度
//这么写没用！
int data_len = 0;//拼接后的数据长度

int index_head = 0; //帧头索引
int index_end = 0;  //帧尾索引

bool HeaderFlag = 0;//找到帧头标志
bool EndFlag = 0;   //找到帧尾标志
bool EntireFlag = 0;//找到完整数据帧标志 

float theta_deg = 0; //转角角度
int theta_deg_rev = 0;//修正后的转角度数
float theta_rad = 0; //转角弧度
bool Flag_RobotYawRate_site = 0; //转弯半径为0标志
//bool EnableRequestFlag = 0;//使能请求帧标志 

/*--------------------------------------函数声明-------------------------------------------------*/

//void write_callback(const std_msgs::String::ConstPtr& msg);
void read_buffer(serial::Serial& ser);
void put_together_init(vector<uint8_t> newread_buffer);
void put_together_remain(int index);

void find_FrameHeader(int search_len);
int refind_FrameHeader();
void find_FrameEnd(int search_len);
int receive_msg_Decode();

int check_EnableRequest();
int serial_init(serial::Serial& ser); 

void RE_receive_msg_Decode(serial::Serial& ser);
int robot_init(serial::Serial& ser);

int robot_move(serial::Serial& ser);
void robot_stop(serial::Serial& ser);

#endif